package com.ksyzt.ksite2.client.news;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.ksyzt.gwt.client.common.MessageComposite;
import com.ksyzt.gwt.client.event.MessageEvent;
import com.ksyzt.ksite2.client.common.Helper;

public class UploadPictures extends MessageComposite {

	private static UploadPicturesUiBinder uiBinder = GWT
			.create(UploadPicturesUiBinder.class);

	interface UploadPicturesUiBinder extends UiBinder<Widget, UploadPictures> {
	}

	public JSONObject returnData;

	private void fireOK() {

		MessageEvent ev = new MessageEvent(MessageEvent.OK, returnData);
		fireEvent(ev);
	}

	private SubmitCompleteHandler m_submit_complete_handler = new SubmitCompleteHandler() {

		@Override
		public void onSubmitComplete(SubmitCompleteEvent event) {

			String jsonstr = event.getResults();

			JSONValue rvalue = JSONParser.parse(jsonstr);
			JSONObject robj = rvalue.isObject();
			JSONString msg = (JSONString) robj.get("err").isString();
			if (msg.stringValue().equals("")) {
				returnData = robj.get("data").isObject();
				fireOK();

			} else {
				message(msg.stringValue());
			}

		}
	};
	private ChangeHandler m_file_change = new ChangeHandler() {

		@Override
		public void onChange(ChangeEvent arg0) {
			String filename = uploader.getFilename();

			if (txtTitle.getValue().length() == 0) {
				if (filename != null && filename.length() > 0) {
					filename = filename.replace("/", "\\");

					String title = "";
					int start = filename.lastIndexOf("\\");
					int end = filename.lastIndexOf(".");
					if (start >= 0) {
						title = filename.substring(start + 1, end);
					} else {
						title = filename.substring(0, end);
					}
					txtTitle.setValue(title);
				}
			}

		}
	};

	public void init(String pnid) {
		form.reset();

		nid.setValue(pnid);
		txtTitle.setValue("");
		txtLink.setValue("");
		message("");
	}

	public UploadPictures() {
		initWidget(uiBinder.createAndBindUi(this));

		form.addSubmitCompleteHandler(m_submit_complete_handler);
		uploader.addChangeHandler(m_file_change);
		form.setEncoding(FormPanel.ENCODING_MULTIPART);
		form.setMethod(FormPanel.METHOD_POST);
		String action = Helper.getTPZXServletPath();
		form.setAction(action);
	}

	@UiField
	FormPanel form;

	@UiField
	Hidden nid;

	@UiField
	FileUpload uploader;

	@UiField
	Label message;

	@UiField
	Button btnCancel;
	@UiField
	TextBox txtTitle;

	@UiField
	TextBox txtLink;

	@UiHandler("btnOK")
	void onOK(ClickEvent e) {

		if (txtTitle.getValue() == null || txtTitle.getValue().equals("")) {
			Window.alert("输入标题");
			return;
		}
		if (uploader.getFilename() == null || uploader.getFilename().equals("")) {
			Window.alert("选择一张图片");
			return;
		}
		form.submit();
	}

	@UiField
	Button btnOK;

	@UiHandler("btnCancel")
	void onCancel(ClickEvent e) {
		MessageEvent ev = new MessageEvent(MessageEvent.CANCEL, 0);
		fireEvent(ev);
	}
}
